package com.skt.common_hr.common.skins
{
    import flash.display.GradientType;
    import flash.display.Graphics;
    import flash.filters.DropShadowFilter;

    import mx.core.EdgeMetrics;
    import mx.graphics.RectangularDropShadow;
    import mx.skins.RectangularBorder;


    /**
     * 화살표 있는 툴팁스킨
     * 화살표의 방향으로 TopLeft, BottomLeft, LeftTop, RightTop 이 있다.
     *
     * @author David Yun(drumcap)
     *
     */
	public class ToolTipSkin extends RectangularBorder
	{
        private var dropShadow:RectangularDropShadow;
        private var _borderMetrics:EdgeMetrics;

        private var pointW:Number = 15;
        private var pointH:Number = 13;

        override public function get borderMetrics():EdgeMetrics
        {
            if (_borderMetrics)
                return _borderMetrics;

            var borderStyle:String = getStyle("tooltipStyle");
            switch (borderStyle)
            {
                case "LeftTop":
                {
                    _borderMetrics = new EdgeMetrics(15, 1, 3, 3);
                    break;
                }

                case "BottomLeft":
                {
                    _borderMetrics = new EdgeMetrics(3, 1, 3, 13);
                    break;
                }

                case "TopLeft":
                {
                    _borderMetrics = new EdgeMetrics(3, 13, 3, 3);
                    break;
                }

                case "RightTop":
                {
                    _borderMetrics = new EdgeMetrics(3, 1, 15, 3);
                    break;
                }

                default: // "toolTip"
                {
                    _borderMetrics = new EdgeMetrics(3, 1, 3, 3);
                    break;
                }
            }

            return _borderMetrics;
        }

        override public function styleChanged(styleProp:String):void
        {
            if (styleProp == "tooltipStyle" || styleProp == "styleName" || styleProp == null)
            {
                _borderMetrics = null;
            }

            invalidateDisplayList();
        }

        override protected function updateDisplayList(w:Number, h:Number):void
        {
            super.updateDisplayList(w, h);

            var tooltipStyle:String = getStyle("tooltipStyle");
            var backgroundColor:uint = getStyle("backgroundColor");
            var backgroundAlpha:Number = getStyle("backgroundAlpha");
            var borderColor:uint = getStyle("borderColor");
            var innerColor:uint = getStyle("innerColor");
            var shadowColor:uint = getStyle("shadowColor");
            var cornerRadius:Number = getStyle("cornerRadius");
            var cornerRadiusInner:Number = getStyle("cornerRadiusInner");
            var color:uint = getStyle("color");
            var borderThickness:int = getStyle("borderThickness");
            var shadowAlpha:Number = 0.1;

            var distance:Number = 8;
            var g:Graphics = graphics;
            g.clear();

            filters = [];

            switch (tooltipStyle)
            {


                case "LeftTop":
                {
                    // border 
                    drawRoundRect(11, 0, w - 11, h - 2, cornerRadius, borderColor, backgroundAlpha, verticalGradientMatrix(11, 0, w - 11, h - 2), GradientType.LINEAR);

                    drawRoundRect(11 + borderThickness, borderThickness, w - 11 - borderThickness * 2, h - 2 - borderThickness * 2, cornerRadiusInner, innerColor, backgroundAlpha, verticalGradientMatrix(11, 0, w - 11, h - 2), GradientType.LINEAR);


                    // left pointer 
                    g.beginFill(borderColor, backgroundAlpha);
                    g.moveTo(11, 7);
                    g.lineTo(0, 13);
                    g.lineTo(11, 19);
                    g.moveTo(11, 7);
                    g.endFill();

                    g.beginFill(innerColor, backgroundAlpha);
                    g.moveTo(11 + borderThickness, 7 + borderThickness);
                    g.lineTo(0 + borderThickness * 2, 13);
                    g.lineTo(11 + borderThickness, 19 - borderThickness);
                    g.moveTo(11 + borderThickness, 7 + borderThickness);
                    g.endFill();

                    filters = [new DropShadowFilter(0, 90, 0, 0.2)];
                    break;
                }

                case "RightTop":
                {
                    // border 
                    drawRoundRect(0, 0, w - 11, h - 2, cornerRadius, borderColor, backgroundAlpha, verticalGradientMatrix(0, 0, w - 11, h - 2), GradientType.LINEAR);

                    drawRoundRect(borderThickness, borderThickness, w - 11 - borderThickness * 2, h - 2 - borderThickness * 2, cornerRadiusInner, innerColor, backgroundAlpha, verticalGradientMatrix(borderThickness, borderThickness, w - 11 - borderThickness * 2, h - 2 - borderThickness * 2), GradientType.LINEAR);


                    // bottom pointer 
                    g.beginFill(borderColor, backgroundAlpha);
                    g.moveTo(w - 11, 7);
                    g.lineTo(w, 13);
                    g.lineTo(w - 11, 19);
                    g.moveTo(w - 11, 7);
                    g.endFill();

                    g.beginFill(innerColor, backgroundAlpha);
                    g.moveTo(w - 11 - borderThickness, 7 + borderThickness);
                    g.lineTo(w - borderThickness * 2, 13);
                    g.lineTo(w - 11 - borderThickness, 19 - borderThickness);
                    g.moveTo(w - 11 - borderThickness, 7 + borderThickness);
                    g.endFill();

                    filters = [new DropShadowFilter(0, 90, 0, 0.2)];
                    break;
                }

                case "BottomLeft":
                {
                    // border 
                    drawRoundRect(0, 0, w, h - 13, cornerRadius, borderColor, backgroundAlpha, verticalGradientMatrix(0, 0, w, h - 13), GradientType.LINEAR);

                    drawRoundRect(borderThickness, borderThickness, w - borderThickness * 2, h - 13 - borderThickness * 2, cornerRadiusInner, innerColor, backgroundAlpha, verticalGradientMatrix(borderThickness, borderThickness, w - borderThickness * 2, h - 13 - borderThickness * 2), GradientType.LINEAR);

                    // bottom pointer 
                    g.beginFill(borderColor, backgroundAlpha);
                    g.moveTo(9, h - 13);
                    g.lineTo(15, h - 2);
                    g.lineTo(21, h - 13);
                    g.moveTo(9, h - 13);
                    g.endFill();

                    g.beginFill(innerColor, backgroundAlpha);
                    g.moveTo(9 + borderThickness, h - 13 - borderThickness);
                    g.lineTo(15, h - 2 - borderThickness * 2);
                    g.lineTo(21 - borderThickness, h - 13 - borderThickness);
                    g.moveTo(9 + borderThickness, h - 13 - borderThickness);
                    g.endFill();

                    filters = [new DropShadowFilter(0, 90, 0, 0.2)];
                    break;
                }

                case "TopLeft":
                {
                    // border 
                    drawRoundRect(0, 11, w, h - 13, cornerRadius, borderColor, backgroundAlpha, verticalGradientMatrix(0, 11, w, h - 13), GradientType.LINEAR);

                    drawRoundRect(borderThickness, 11 + borderThickness, w - borderThickness * 2, h - 13 - borderThickness * 2, cornerRadiusInner, innerColor, backgroundAlpha, verticalGradientMatrix(borderThickness, 11 + borderThickness, w - borderThickness * 2, h - 13 - borderThickness * 2), GradientType.LINEAR);

                    // top pointer 
                    g.beginFill(borderColor, backgroundAlpha);
                    g.moveTo(9, 11);
                    g.lineTo(15, 0);
                    g.lineTo(21, 11);
                    g.moveTo(10, 11);
                    g.endFill();

                    g.beginFill(innerColor, backgroundAlpha);
                    g.moveTo(9 + borderThickness, 11 + borderThickness);
                    g.lineTo(15, 0 + borderThickness * 2);
                    g.lineTo(21 - borderThickness, 11 + borderThickness);
                    g.moveTo(10 + borderThickness, 11 + borderThickness);
                    g.endFill();

                    filters = [new DropShadowFilter(0, 90, 0, 0.2)];
                    break;
                }


                default:
                {
                    // face
                    drawRoundRect(3, 1, w - 6, h - 4, cornerRadius, backgroundColor, backgroundAlpha)

                    if (!dropShadow)
                        dropShadow = new RectangularDropShadow();

                    dropShadow.distance = 3;
                    dropShadow.angle = 90;
                    dropShadow.color = 0;
                    dropShadow.alpha = 0.4;

                    dropShadow.tlRadius = cornerRadius + 2;
                    dropShadow.trRadius = cornerRadius + 2;
                    dropShadow.blRadius = cornerRadius + 2;
                    dropShadow.brRadius = cornerRadius + 2;

                    dropShadow.drawShadow(graphics, 3, 0, w - 6, h - 4);

                    break;
                }
            }
        }
	}
}